//
//  YZMStockTotalViewController.m
//  YZMicroStock
//
//  Created by apple on 2017/12/26.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMStockTotalViewController.h"
#import "YZMStockViewController.h"
#import "YZMTradeBasicViewController.h"
#import "YZMControllerManager.h"
#import "YZMWebViewService.h"

@interface YZMStockTotalViewController ()

@property (nonatomic, strong) YZMStockViewController *stockVc;

@property (nonatomic, strong) YZMStockTDViewController *stockTDVc;

@property (nonatomic, assign) NSUInteger scroolIndex_T_1;

@property (nonatomic, assign) NSUInteger scroolIndex_T_D;

@property (nonatomic, strong) UISegmentedControl *segment;

@end

@implementation YZMStockTotalViewController

- (instancetype)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        _segment = [[UISegmentedControl alloc] initWithItems:[@[sYZMTradeNameForAStock ,sYZMTradeNameForAStockD] mutableCopy]];
        _segment.frame = CGRectMake(0, 0, 64, 32);
        _segment.layer.cornerRadius = 16;
        _segment.layer.borderWidth = 1;
        _segment.layer.borderColor = [YZMAppStyle colorForBoder].CGColor;
        _segment.clipsToBounds = YES;
        _segment.tintColor = [YZMAppStyle colorForIconColor];
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
        [_segment setWidth:90 forSegmentAtIndex:0];
        [_segment setWidth:90 forSegmentAtIndex:1];
        [_segment addTarget:self action:@selector(segmentPressed:) forControlEvents:UIControlEventValueChanged];
        _segment.selectedSegmentIndex = 0;
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.titleView = self.segment;
    self.navigationItem.rightBarButtonItem = [YZMAppStyle rightBarItemWithTitle:@"规则" target:self action:@selector(rightBarItemPressed:)];
}

- (void)rightBarItemPressed:(id)sender{
    NSInteger tradeType = self.stockVc.view.hidden ? YZM_TRADE_TYPE_FOR_A_STOCK_D : YZM_TRADE_TYPE_FOR_A_STOCK;
    [YZMWebViewService openTradeRule:tradeType];
}

- (void)segmentPressed:(UISegmentedControl *)seg{
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.stockVc.view.hidden = NO;
            self.stockTDVc.view.hidden = YES;
            [self.stockVc viewWillAppear:YES];
            break;
        case 1:
            self.stockVc.view.hidden = YES;
            self.stockTDVc.view.hidden = NO;
            [self.stockTDVc viewWillAppear:YES];
            break;
        default:
            break;
    }
}

- (void)updateWithParams: (NSDictionary *)params{
    self.scroolIndex_T_1 = [[params objectForKey:scrollIndexTotalOne] integerValue];
    self.scroolIndex_T_D = [[params objectForKey:scrollIndexTotalTD] integerValue];
    if (self.scroolIndex_T_1 == 0 && self.scroolIndex_T_D == 1){
        self.segment.selectedSegmentIndex = 1;
    }
    [self segmentPressed:self.segment];
}

+ (instancetype)createWithParams: (NSDictionary *)params{
    YZMStockTotalViewController *ctrl = [[self alloc] init];
    if ([params.allKeys containsObject:@"segment"]) {
        ctrl.segment.selectedSegmentIndex = 1;
    }
    Class ctrlClassStock = [[YZMControllerManager sharedManager] classForControllerName: @"stock"];
    if ([(NSObject *)ctrlClassStock respondsToSelector: @selector(createWithParams:)]) {
        ctrl.stockVc = [ctrlClassStock createWithParams: params];
    }
    
    Class ctrlClassStockTD = [[YZMControllerManager sharedManager] classForControllerName: @"stockTD"];
    if ([(NSObject *)ctrlClassStockTD respondsToSelector: @selector(createWithParams:)]) {
        ctrl.stockTDVc = [ctrlClassStockTD createWithParams: params];
    }
    [ctrl addChildViewController:ctrl.stockTDVc];
    ctrl.stockTDVc.view.frame = ctrl.view.bounds;
    [ctrl.view addSubview:ctrl.stockTDVc.view];
    
    [ctrl addChildViewController:ctrl.stockVc];
    ctrl.stockVc.view.frame = ctrl.view.bounds;
    [ctrl.view addSubview:ctrl.stockVc.view];
//    [ctrl segmentPressed:ctrl.segment];
    [ctrl updateWithParams: params];
    
    return ctrl;
}


- (void)showSellListPage{
    if (!self.stockVc.view.hidden) {
        [self.stockVc showSellListPage];
        return;
    }
    if (!self.stockTDVc.view.hidden) {
        [self.stockTDVc showSellListPage];
        return;
    }
}

- (void)updateWithParamsManual: (NSDictionary *)params{
    [self.stockVc updateWithParamsManual:params];
    [self.stockTDVc updateWithParamsManual:params];
}

- (void)showEntrustListPage{
    if (!self.stockVc.view.hidden) {
        [self.stockVc showEntrustListPage];
        return;
    }
    if (!self.stockTDVc.view.hidden) {
        [self.stockTDVc showEntrustListPage];
        return;
    }
    
}

- (void)showSettlementListPage{
    if (!self.stockVc.view.hidden) {
        [self.stockVc showSettlementListPage];
        return;
    }
    if (!self.stockTDVc.view.hidden) {
        [self.stockTDVc showSettlementListPage];
        return;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
